package com.android.test;

import java.util.HashSet;
import java.util.Random;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.net.Uri;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

public class GameLoop extends View implements Runnable {
	
	private static final int INTERVAL = 10;
	private static final String TAG = "JAGA GAME VIEW";
	public static Context context;
	
	private boolean running = true;
	
	private static int screenWidth;
	private static int screenHeight;
	private Random rand;
	
	private PointF[][] positions = new PointF[3][3];
	private PointF currentMolePosition;
	public HashSet<GameObject> listGameObjects = new HashSet<GameObject>();
	public Mole mole;
	public Background background;
	public RestartButton restart;
	public FormsButton forms;
	private int score;
	
	private long counter = 0;
	private long realTime = 0;
	private long initialTime = 0;
	private float interval = 2;
	
	private boolean playing = false;
	
	private Intent browserIntent;
	private Paint textPaint;
	
	public GameLoop(Context context,Resources resources) 
	{
		super(context);
		GameLoop.context = context;
		
		running = true;		
		
		this.setBackgroundColor(Color.WHITE);
		
		setFocusableInTouchMode(true);
		setClickable(true);
		setLongClickable(true);
		
		Thread gameUpdate = new Thread(this);
		gameUpdate.start();
	}
		
	private void start() 
	{
		playing = true;
		initialTime = (System.currentTimeMillis() / 1000) + 30;
		
		textPaint = new Paint();
		textPaint.setTextSize(screenWidth/10);
		textPaint.setColor(Color.WHITE);
		
		rand = new Random();
		
		background = new Background(new PointF(0,0), screenWidth, screenHeight);
		
		for (int y = 1; y <= 3; y++) 
		{
			for(int x = 1; x <= 3; x++)
			{
				positions[x - 1][y - 1] = new PointF(screenWidth / 4 * x, screenHeight / 4 * y);
				Hole temp = new Hole(positions[x - 1][y - 1], screenWidth/5, screenWidth/5);
				listGameObjects.add(temp);
			}
		}
		
		currentMolePosition = new PointF(rand.nextInt(3), rand.nextInt(3));
				
		mole = new Mole(positions[(int) currentMolePosition.x][(int) currentMolePosition.y], screenWidth/7, screenWidth/7);
		restart = new RestartButton(new PointF(screenWidth/4 - (screenWidth/5)/2, screenHeight - screenWidth/4), screenWidth/5, screenWidth/5);
		forms = new FormsButton(new PointF(screenWidth/4 * 3 - (screenWidth/5)/2, screenHeight - screenWidth/4), screenWidth/5, screenWidth/5);
	}

	@Override
	public void run() 
	{
		Log.i(TAG, "iniciou a trhead");
		
		while (running) 
		{
			try 
			{
				Thread.sleep(INTERVAL);
			} 
			catch (InterruptedException e) 
			{
				Log.e(TAG, "main loop finished");
			}
			
			update();
			postInvalidate();
		}
	}

	private void update() 
	{
		if (playing)
		{
			realTime = System.currentTimeMillis() / 1000;
			
			if (System.currentTimeMillis() / 1000 - counter >= interval)
			{
				changeMolePosition();
			}
			
			if (initialTime - realTime == 0)
			{
				playing = false;
			}
		}
	}

	public void draw(Canvas canvas)
	{
		super.draw(canvas);

		background.draw(canvas);
		
		canvas.drawText("Score:" + score, screenWidth/23, screenHeight/8, textPaint);
		canvas.drawText("Time:" + (initialTime - realTime), screenWidth  - screenWidth/2, screenHeight/8, textPaint);
		
		if(listGameObjects.size() > 0)
		{
			for (GameObject go : listGameObjects) 
			{
				go.draw(canvas);
			}
		}
		
		if (playing)
		{
			mole.draw(canvas);	
		}
		
		restart.draw(canvas);
		forms.draw(canvas);
	}
	
	public void restartGame()
	{
		initialTime = (System.currentTimeMillis() / 1000) + 30;
		score = 0;
		interval = 2;
		playing = true;
	}
	
	public void changeMolePosition ()
	{
		if(mole != null)
		{
			PointF newPosition = new PointF(rand.nextInt(3), rand.nextInt(3));
			
			if (interval > 0.5f)
			{
				interval -= 0.1f;
			}
			
			if(currentMolePosition == newPosition)
			{
				if(currentMolePosition.x < 2)
				{
					currentMolePosition = mole.setPosition(positions[(int) (currentMolePosition.x + 1)][(int) currentMolePosition.y]);
				}
				else
				{
					currentMolePosition = mole.setPosition(positions[(int) (currentMolePosition.x - 1)][(int) currentMolePosition.y]);
				}
			}
			else
			{
				currentMolePosition = mole.setPosition(positions[(int) newPosition.x][(int) newPosition.y]);
			}

			counter = System.currentTimeMillis() / 1000;
		}
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) 
	{
		// primeiro toque na tela
		if (event.getAction() == MotionEvent.ACTION_DOWN) 
		{
			PointF point = new PointF(event.getX(), event.getY());
			
			if(playing)
			{
				if(point.x > (mole.position.x - mole.width) && point.x < (mole.position.x + mole.width))
				{
					if(point.y > (mole.position.y - mole.height) && point.y < (mole.position.y + mole.height))
					{
						changeMolePosition();
						score++;
					}
				}
			}
			
			if(point.x > restart.position.x && point.x < (restart.position.x + restart.width))
			{
				if(point.y > restart.position.y && point.y < (restart.position.y + restart.height))
				{
					restartGame();
				}
			}
			
			if(point.x > forms.position.x && point.x < (forms.position.x + forms.width))
			{
				if(point.y > forms.position.y && point.y < (forms.position.y + forms.height))
				{
					browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://bit.ly/ZLV3XC"));
					context.startActivity(browserIntent);
				}
			}
		}
	
		return super.onTouchEvent(event);
	}
	
	protected void onSizeChanged(int w, int h, int oldw, int oldh) 
	{
		super.onSizeChanged(w, h, oldw, oldh);
		screenHeight = getHeight();
		screenWidth = getWidth();
		
		start();
	}
	
	public static int getScreenWidth()
	{
		return screenWidth;
	}
	
	public static int getScreenHeight()
	{
		return screenHeight;
	}
	
	public void onDestroy()
	{
		running = false;
	}
}
